#!/bin/sh /etc/rc.common
# This file is not compatible with White Russian and Kamikaze.
#
# identification of the device
#
START=90

dmesgbackup_config() {
	local cfg="$1"
	local enabled file gzip GZIP_CMD
	local DEFAULT_enabled=0
	local DEFAULT_file="/var/log/dmesg"
	local DEFAULT_gzip=1

	config_get_bool enabled "$cfg" enabled "$DEFAULT_enabled"
	[ "$enabled" -eq 1 ] && {
		[ ! -f "$file" ] && {
			config_get file "$cfg" file
			file=${file:-$DEFAULT_file}
			config_get_bool gzip "$cfg" gzip "$DEFAULT_gzip"
			[ "$gzip" -eq 1 ] && {
				dmesg 2>/dev/null | gzip -c - >"$file.gz"
			} || {
				dmesg 2>/dev/null >"$file"
			}
		}
	}
}

start() {
. /lib/config/uci.sh
. /usr/lib/webif/functions.sh
uci_load "webif"

	#detect device
	device_name="$CONFIG_general_device_name"
	
	[ -z "$device_name" ] && [ -f "/proc/diag/model" ] && device_name=$(cat "/proc/diag/model")
	while [ -z "$device_name" ]; do
		uname -a |grep -q "mips"
		[ $? = "0" ] && {
	        	dd bs=1 count=19 skip=5 if=/dev/mtd/5 2>/dev/null | strings | grep "MR3201A-FLF-FON" || ifconfig eth0 |grep -q 00:18:84:**:**:**
			[ $? = "0" ] && {
				device_name="La Fonera (ACCTON MR3201A)"
				break
			}
			ifconfig eth0 |grep -q 00:18:0A:**:**:**
			[ $? = "0" ] && {
				device_name="Meraki Mini"
				break
			}
			cat /proc/cpuinfo |grep -q "WP54"
			[ $? = "0" ] && {
				device_name="Compex WP54"
				break
			}
		}

		if [ "$(uname -m)" = "i?86" ]; then
			ifconfig eth0 |grep -q 00:0D:B9:**:**:**
			[ $? = "0" ] && {
				device_name="PC Engines WRAP"
				break
			}
		fi

		cat /proc/cpuinfo |grep -q "IXP4\?\?"
		[ $? = "0" ] && {
			cat /proc/cpuinfo |grep "Gateway 7001 AP"
			[ $? = "0" ] && {
				device_name="Gateway 7001 AP"
				break
			}
			cat /proc/cpuinfo |grep "D-Link DSM-G600 RevA"
			[ $? = "0" ] && {
				device_name="D-Link DSM-G600 RevA"
				break
			}
			cat /proc/cpuinfo |grep "Freecom FSG-3"
			[ $? = "0" ] && {
				device_name="Freecom FSG-3"
				break
			}
			cat /proc/cpuinfo |grep "Iomega NAS 100d"
			[ $? = "0" ] && {
				device_name="Iomega NAS 100d"
				break
			}
			cat /proc/cpuinfo |grep "Linksys NSLU2"
			[ $? = "0" ] && {
				device_name="Linksys NSLU2"
				break
			}
		}
		break
	done
	if [ -z "$CONFIG_general_firmware_version" -o -z "$CONFIG_general_firmware_name" ]; then
		#detect firmware info
		firmware_name=$CONFIG_general_firmware_name
		firmware_subtitle=$CONFIG_general_firmware_subtitle
		firmware_version=$CONFIG_general_firmware_version

		if grep -qi "KAMIKAZE" "/etc/banner"; then
			firmware_name="OpenWrt Kamikaze"
		else	
			firmware_name="Unknown Wrt"
		fi	
		firmware_subtitle="With X-Wrt Extensions"

		if [ -f "/etc/openwrt_version" ]; then
			firmware_version=$(cat "/etc/openwrt_version")
		elif grep -q "KAMIKAZE" "/etc/banner"; then
			firmware_version=$(cat /etc/banner |grep KAMIKAZE |cut -d '(' -f 2 |cut -d ')' -f 1)
		else
			firmware_version="unknown"
		fi

		#
		# if any variables changed, commit the change
		#

		if [ "$CONFIG_general_firmware_version" != "$firmware_version" ] ||
			[ "$CONFIG_general_firmware_name" != "$firmware_name" ] ||
			[ "$CONFIG_general_firmware_subtitle" != "$firmware_subtitle" ]; then
			echo "Committing new firmware id ..."
			uci_set "webif" "general" "firmware_name" "$firmware_name"
			uci_set "webif" "general" "firmware_version" "$firmware_version"
			uci_set "webif" "general" "firmware_subtitle" "$firmware_subtitle"
		fi
	fi

	[ "$device_name" != "$CONFIG_general_device_name" ] && {
		echo "Device: $device_name" 	
		echo "Committing new device id ..."		
		uci_set "webif" "general" "device_name" "$device_name"
	}

	if [ -e "/tmp/.uci/webif" ]; then
		uci_commit "webif"
	fi

	config_load syslog
	config_foreach dmesgbackup_config dmesgbackup
}